package com.yj.springbootmybatis.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;

@Configuration
public class CorsConfig implements HandlerInterceptor {


    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("*"); //允许所有的访问请求路径
        configuration.addAllowedMethod("*"); //允许所有的方法，设置允许的请求方式
        configuration.addAllowedHeader("*"); //允许所有的请求头，设置允许的header
        configuration.setAllowCredentials(true); //允许传递凭证信息，设置是否发送cookie信息

        // 为url 添加映射路径
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",configuration);

        // 返回重新定义好的corsSource
        CorsFilter corsFilter = new CorsFilter(corsConfigurationSource);
        return corsFilter;
    }


}
